<template>
  <div class="shopping-cart">
    <el-card>
      <el-row :gutter="25">
        <!-- 用户选择框 -->
        <el-col :span="8">
          <el-select v-model="uid" placeholder="请选择用户" clearable @clear="findPage">
            <el-option v-for="u in usersList" :label="u.name" :value="u.id" style="width: 300px; margin-bottom: 20px;"></el-option>
          </el-select>
        </el-col>
        <!-- 查询按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="findPage">查询</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table :data="ShopPage" style="width: 100%">
      <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
      <el-table-column prop="price" label="单价" width="180"></el-table-column>
      <el-table-column label="图片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.photo" alt="商品图片" width="50">
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="180"></el-table-column>
      <el-table-column prop="number" label="数量" width="180"></el-table-column>
      <el-table-column prop="uname" label="用户名" width="180"></el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[3, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 3,
      ShopPage: [],
      total: 0,
      uid: '', // 默认为空字符串，表示未选择用户
      usersList: [],
    };
  },
  methods: {
    findPage() {
      axios.get(`/buy/shopping/findShopPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}&uid=${this.uid}`).then(res => {
        this.ShopPage = res.data.data.page.records;
        this.total = res.data.data.page.total;
        console.log(this.total)
        if (this.total >res.data.data.page.records.pages){
          this.pageNum=res.data.data.page.pages
        }
      });
    },
    findUsersVo() {
      axios.get('/buy/shopping/findUsersVo').then(res => {
        this.usersList = res.data.data.users;
        console.log(this.usersList);
      });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.findPage();
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage;
      this.findPage();
    },
  },
  created() {
    this.findPage();
    this.findUsersVo();
  }
};
</script>

<style scoped>
.shopping-cart {
  margin: 20px;
}
</style>
